
#include "gtree"

namespace mc
{
    gleaf::gleaf(std::string& str_ori, char sz_sep1)
        :_str_label("")
    {
        to_leaf(str_ori, sz_sep1);
    }

    gleaf::~gleaf()
    {
        _tml.clear();
    }

    void gleaf::to_leaf(std::string& str_ori, char sz_sep1)
    {
        if ("" == str_ori)
        {
            return;
        }

        std::string str_newori = str_ori;
        size_t idh_index = std::string::npos;
        size_t idy_index = std::string::npos;
        std::string str_unit("");

        while ("" != str_newori)
        {
            idh_index = str_newori.find_first_of(sz_sep1);
            if (std::string::npos != idh_index)
            {
                str_unit   = str_newori.substr(0, idh_index);
                str_newori = str_newori.substr(idh_index + 1);
            }
            else
            {
                str_unit   = str_newori;
                str_newori = "";
            }

            idy_index = str_unit.find('=');
            if (std::string::npos == idy_index)
            {
                continue;
            }
            std::string str_name  = str_unit.substr(0, idy_index);
            std::string str_value = str_unit.substr(idy_index + 1);

            _tml.update_t2(str_name, str_value);
        }
    }
    
    void gleaf::to_leaf(gleaf& o)
    {
        for (auto& iter : o._tml._m_1)
        {
            std::string str_1(iter.first);
            _tml.update_t2(str_1, iter.second);
        }
    }

    std::string gleaf::to_str()
    {
        std::ostringstream ostr1("");
        bool bdh = false;
        for (auto iter : _tml._m_1)
        {
            if (true == bdh)
            {
                ostr1 << ',';
            }
            bdh = true;
            ostr1 << (iter.first) << '=' << (iter.second);//value2str(iter.second);
        }
        return ostr1.str();
    }

    std::string gleaf::get_str(const std::string &str_name)
    {
        std::string str_k(str_name);
        return _tml.get_t2(str_k, (std::string)"");
    }

    double gleaf::get_double(const std::string &str_name)
    {
        std::string str_k(str_name);
        std::string str_value = _tml.get_t2(str_k, (std::string)"");
        if ("" == str_value) { return 0; }

        return std::stod(str_value);
    }

    int gleaf::get_int(const std::string& str_name)
    {
        std::string str_k(str_name);
        std::string str_value = _tml.get_t2(str_k, (std::string)"");
        if ("" == str_value) { return 0; }

        return std::stoi(str_value);
    }
}

